import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: "/login",
    component: () => import("@/views/Login.vue"),
    name: "login"
  },
  {
    path: "/home",
    component: () => import("@/views/Home.vue"),
    name: "home",
    children: [
      {
        path: "/users",
        component: () => import("@/components/users/users.vue"),
        name: "users"
      },
      {
        path: "/rights",
        component: () => import("@/components/rights/rights.vue"),
        name: "rights"
      },
      {
        path: "/roles",
        component: () => import("@/components/rights/roles.vue"),
        name: "roles"
      },
    ]
  },
  {
    path: "/",
    redirect: localStorage.getItem("redirectpath") ? localStorage.getItem("redirectpath") : "/home"
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})




import store from "@/store/index.js"

router.beforeEach((to, from, next) => {
  if (to.path == "/login") {
    next();
  } else {

    if (store.getters.token) {
      next();
    } else {
      next("/login")
    }

  }
})

export default router
